package com.ytjj.bigdata.service;

import com.github.pagehelper.PageHelper;
import com.ytjj.bigdata.dao.AdsAppChannelMapper;
import com.ytjj.bigdata.entity.AdsAppChannel;
import com.ytjj.bigdata.model.dto.AdsAppChannelDto;
import com.ytjj.bigdata.model.response.AdsAppChannelResponse;
import com.ytjj.common.api.CommonPageExtend;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class AdsAppChannelService {
    @Resource
    private AdsAppChannelMapper adsAppChannelMapper;

    public CommonPageExtend selectAdsAppChannel(AdsAppChannelDto adsAppChannelDto) {
        PageHelper.startPage(adsAppChannelDto.getPageNum(),adsAppChannelDto.getPageSize());
        List<AdsAppChannel> list = adsAppChannelMapper.selectAdsAppChannelAll(adsAppChannelDto);

        if (CollectionUtils.isEmpty(list)) {
            return new CommonPageExtend();
        }

        list.stream()
                .filter(item -> 0 != item.getRegRate())
                .forEach(item -> {
                    item.setRegRate(new BigDecimal(item.getRegRate() * 100).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
                });

        list.stream()
                .filter(item -> 0 != item.getRegActRate())
                .forEach(item -> {
                    item.setRegActRate(new BigDecimal(item.getRegActRate() * 100).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
                });

//        list.stream()
//                .filter(item -> 0 != item.getActivation())
//                .forEach(item -> {
//                    double regRate = new BigDecimal(item.getRegister()/item.getActivation()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
//                    item.setRegRate(regRate);
//                });
//        list.stream()
//                .filter(item -> 0 != item.getActive())
//                .forEach(item -> {
//                    double regActRate = new BigDecimal(item.getRegister()/item.getActive()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
//                    item.setRegActRate(regActRate);
//                });

        CommonPageExtend<AdsAppChannel> pageInfo = CommonPageExtend.restPage(list);

        List<AdsAppChannelResponse> appChannelResponses = adsAppChannelMapper.findAdsAppChannel(adsAppChannelDto);
        AdsAppChannelResponse response = appChannelResponses.get(0);

        pageInfo.setActivationCount(response.getActivationCount());
        pageInfo.setRegisterCount(response.getRegisterCount());
        pageInfo.setEnablementCount(response.getEnablementCount());
        pageInfo.setActiveCount(response.getActiveCount());
        // double
        // 注册转化率 (注册量 / 激活量)
        if (response.getActivationCount() > 0) {
            double regRate = new BigDecimal(response.getRegisterCount() * 100 / response.getActivationCount())
                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            pageInfo.setRegRateCount(regRate);
        }
        // 注册活跃用户占比 (注册量 / 活跃用户量)
        if (response.getActiveCount() > 0) {
            double regActRate = new BigDecimal(response.getRegisterCount() * 100 / response.getActiveCount())
                    .setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            pageInfo.setRegActRateCount(regActRate);
        }
//        pageInfo.setRegRateCount(new BigDecimal(response.getRegRateCount()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
//        pageInfo.setRegActRateCount(new BigDecimal(response.getRegActRateCount()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());

        return pageInfo;
    }
}
